$(function () {
    //功能1 点击盒子隐藏，显示隐藏盒子
    $('#login-box a').on('click', function () {
        //点击登录隐藏
        
        $('#login-box').hide()
        $('#reg-box').show()
    })

    $('#reg-box a').on('click', function () {
        //点击注册隐藏
        // console.log(11)
        $('#login-box').show()
        $('#reg-box').hide()
    })

//input框的输入条件判断
let form = layui.form
form.verify({
    username: [
        /^[a-zA-Z0-9]{1,10}$/,
        "用户名为1~10位，由数字和英文字母组成"
    ],
    psw: [
        /^[a-zA-Z0-9]{6,12}$/,
        "密码由6到12位数字和字母组成"
    ],
    repsw: function (value) {
        let psw = $('#formreg input[name=password]').val()
        if (value != psw) {
            return  "两次输入的密码不一致"
        }

    }
})



    //注册提交按钮
    $('#formreg').on('submit', function (e) {
        e.preventDefault()
        axios({
            url:'/api/reguser',
            method:'post',
            data: $(this).serialize(),
            
        }).then(({data:res})=>{
            //成功回调
            console.log(res)
            if(res.status != 0){
               return layer.msg(res.message)
            }
            layer.msg(res.message)
            $('#reg-box a').click()
            $('#formreg')[0].reset()
        });
    })

    $('#formlog').on('submit',function(e){
        e.preventDefault()
        axios({
            url:'/api/login',
            method:'post',
            data: $(this).serialize() ,
        }).then(({data:res})=>{
            //成功回调
            console.log(res)
            if(res.status != 0){
                return  layer.msg(res.message)
            }
            layer.msg(res.message)
            localStorage.setItem('token',res.token)
            location.href = '/index.html'
        });
    })
})